#include "mainwindow.h"
#include <QDebug>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
#include "kcf/kcftracker.hpp"

using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{

    cout << "Built with OpenCV " << CV_VERSION << endl;
    Mat frame;
    VideoCapture capture;
    capture.open("E:\\opencv\\sources\\doc\\js_tutorials\\js_assets\\cup.mp4");

    // 读取第一帧并选择跟踪区域
    capture >> frame;
    if (frame.empty()) {
        qWarning("Could not read frame");
        return -1;
    }

    cv::Rect2d bbox = cv::selectROI("Tracker", frame, false, false);
    if (bbox.width == 0 || bbox.height == 0) {
        qWarning("Invalid bounding box");
        return -1;
    }
    KCFTracker tracker(true, false, true, false);
    Rect result;
    // 初始化KCF跟踪器
    tracker.init(bbox, frame);
    if(capture.isOpened())
    {
        cout << "Capture is opened" << endl;
        for(;;)
        {
            capture >> frame;
            if(frame.empty())
                break;
            result = tracker.update(frame);
            rectangle( frame, Point( result.x, result.y ), Point( result.x+result.width, result.y+result.height), Scalar( 0, 0, 255 ), 2, 8 );
            imshow("Sample", frame);
            if(waitKey(10) >= 0)
                break;
        }
    }

    return 0;
}
